Shader "Unlit/Advert"
{
    Properties
    {
        _MainTex("Main Tex", 2D) = "white" {}
        _Color("Color", Color) = (1, 1, 1, 1)
        _VerticalBillboarding("Vertical Restraints", Range(0, 1)) = 1 //物体法线方向正对视角方向，还是y轴方向，或是两者中间的某个向量（1为视角方向，0为y轴方向）
        _IsBuild("IsBuild", Int) = 0
    }
    SubShader
    {
        LOD 200
        Tags
        {
            "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "DisableBatching" = "True"
        }
        //DisableBatching标签：设置为True后就不会对此Shader相关的模型进行批处理
        //这是因为我们在顶点着色器中修改了模型空间下物体的顶点，而批处理本质上是将多个物体合成一个物体的逻辑，这样对于单个模型就失去它的模型空间了
        PASS
        {
            Tags
            {
                "LightMode" = "ForwardBase"
            }
            ZWrite Off Cull Off
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert             //声明顶点着色器的函数
            #pragma fragment frag           //声明片段着色器的函数
            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTex;
            fixed4 _Color;
            float _VerticalBillboarding;
            float4 _MainTex_ST;

            float _IsBuild = false;
            float3x3 rotationMatrix;

            struct _2vert
            {
                float4 vertex: POSITION;
                float4 texcoord: TEXCOORD0;
            };

            struct vert2frag
            {
                float4 pos: SV_POSITION;
                float2 uv: TEXCOORD0;
            };

            vert2frag vert(_2vert v)
            {
                float3 center = float3(0, 0, 0); //模型空间中，物体的中心点（锚点，物体围绕其旋转）
                float3 viewer = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
                float3 normalDir = viewer - center; //获取物体到摄像机的方向
                normalDir.y = normalDir.y * _VerticalBillboarding;
                normalDir = normalize(normalDir); //得到物体的法线，第一个轴已确定

                float3 upDir = abs(normalDir.y) > 0.999 ? float3(0, 0, 1) : float3(0, 1, 0);
                //如果物体法线与上向量重合，那么很显然只能借用右向量来求第二个轴，否则借用上向量来求第二个轴
                float3 rightDir = normalize(cross(upDir, normalDir)); //先借用右向量或者上向量，叉乘算出第二个轴
                upDir = normalize(cross(normalDir, rightDir)); //已知两个轴，叉乘得到第三个轴
                float3 centerOffs = v.vertex.xyz - center;
                rotationMatrix = float3x3
                (
                    rightDir.x, upDir.x, normalDir.x,
                    rightDir.y, upDir.y, normalDir.y,
                    rightDir.z, upDir.z, normalDir.z
                );

                
                float3 rotatedOffset = mul(rotationMatrix, float3x1(centerOffs.xyz));
                float3 localPos = rotatedOffset;
                // float3 localPos = center + rotatedOffset;
                // float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir * centerOffs.z;


                vert2frag o;
                o.pos = UnityObjectToClipPos(float4(localPos, 1));
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }

            fixed4 frag(vert2frag i): SV_Target
            {
                fixed4 color = tex2D(_MainTex, i.uv);
                color.rgb *= _Color.rgb;
                return color;
            }
            ENDCG
        }
    }
}